﻿using FirstFloor.ModernUI.Presentation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelElectCalcModernUI.ViewModel.Home
{
    public class HomeDataModel : NotifyPropertyChanged
    {
        double totalPower;
        double standardSellerPrice = 0;
        double standardFactoryPrice = 0;
        double turnoverRatio = 80;
        ObservableCollection<PowerFactory> powerFactories;
        ObservableCollection<PowerSeller> powerSellers;

        public HomeDataModel() { }

        public double TotalPower
        {
            get
            {
                return totalPower;
            }

            set
            {
                totalPower = value;
                OnPropertyChanged("TotalPower");
            }
        }

        public double StandardSellerPrice
        {
            get
            {
                return 0;
                //return standardSellerPrice;
            }

            set
            {
                standardSellerPrice = value;
                OnPropertyChanged("StandardSellerPrice");
                standardFactoryPrice = value;
                OnPropertyChanged("StandardFactoryPrice");
            }
        }

        public double StandardFactoryPrice
        {
            get
            {
                return 0;
                //return standardFactoryPrice;
            }

            set
            {
                standardFactoryPrice = value;
                OnPropertyChanged("StandardFactoryPrice");
                standardSellerPrice = value;
                OnPropertyChanged("StandardSellerPrice");
            }
        }

        public ObservableCollection<PowerFactory> PowerFactories
        {
            get
            {
                if (powerFactories == null)
                    powerFactories = new ObservableCollection<PowerFactory>();
                powerFactories.CollectionChanged += ((s, e) =>
                    PropChange(e, "FactoryTotal", "FactorySellerTot"));
                return powerFactories;
            }
        }

        private void PropChange(NotifyCollectionChangedEventArgs e, params string[] noticProp)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (var item in e.NewItems)
                    {
                        if (item is PowerFactory)
                        {
                            (item as PowerFactory).PropertyChanged += ((ss, ee) =>
                            {
                                foreach(var p in noticProp)
                                { 
                                    OnPropertyChanged(p);
                                }
                            });
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                case NotifyCollectionChangedAction.Replace:
                case NotifyCollectionChangedAction.Reset:
                    foreach (var p in noticProp)
                    {
                        OnPropertyChanged(p);
                    }
                    break;
            }
        }

        public ObservableCollection<PowerSeller> PowerSellers
        {
            get
            {
                if (powerSellers == null)
                    powerSellers = new ObservableCollection<PowerSeller>();
                powerFactories.CollectionChanged += ((s, e) =>
                    PropChange(e, "SellerTotal", "FactorySellerTot"));
                return powerSellers;
            }
        }
        public double FactoryTotal
        {
            get
            {
                return powerFactories.Sum(s=>s.Power);
            }
        }
        public double SellerTotal
        {
            get
            {
                return powerSellers.Sum(s=>s.Power);
            }
        }
        public double FactorySellerTot
        {
            get
            {
                return powerFactories.Sum(s => s.Power) + powerSellers.Sum(s => s.Power);
            }
        }
        public double TurnoverRatio
        {
            get
            {
                return turnoverRatio;
            }

            set
            {
                turnoverRatio = value;
                OnPropertyChanged("TurnoverRatio");
            }
        }
    }
}
